/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.method;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import z.h.w.jar.data.time.Time;

/**
 *
 * @author EnzoZhong
 * @date 2011.09.15
 */
public class To {

      final private static String[] STRING_REGEX = new String[] {
            "yyyy" , "yyyyMM" , "yyyyMMdd" , "yyyyMMdd HH" , "yyyyMMdd HHmm" , "yyyyMMdd HHmmss" ,
            "yyyy-MM-dd HH:mm:ss" };

      /**
       * 范型数组转成范型List
       *
       * @param <Type>
       * @param array
       *
       * @return
       */
      public static synchronized <Type> List<Type> array2List ( Type[] array ) {
            List<Type> list = new ArrayList<> ( array.length + 2 );
            list.addAll ( Arrays.asList ( array ) );
            return list;
      }

      public static synchronized Locale string2Locale ( String lanCode ) {
            String[] array = lanCode.split ( "_" );
            return new Locale ( array[0] , array[1] );
      }

      public static synchronized <Type> Type[] collection2Array ( Collection<Type> coll ) {
            List<Type> list = new ArrayList<> ( 10 );
            for ( Type t : coll ) {
                  list.add ( t );
            }
            return list2Array ( list );
      }

      public static synchronized <Type> List<Type> collection2List ( Collection<Type> coll ) {
            List<Type> list = new ArrayList<> ( 10 );
            for ( Type t : coll ) {
                  list.add ( t );
            }
            return list;
      }

      /**
       *
       * @param <Type>
       * @param array
       *
       * @return
       */
      public static synchronized <Type> Stack<Type> array2Stack ( Type[] array ) {
            Stack<Type> stack = new Stack<> ();
            stack.addAll ( Arrays.asList ( array ) );
            return stack;
      }

      /**
       * 数组转为String
       * <p/>
       * @param <Type>
       * @param array
       *
       * @return
       */
      public static synchronized <Type> String array2String ( Type[] array ) {
            StringBuffer sb = new StringBuffer ();
            for ( Type t : array ) {
                  sb.append ( t ).append ( " " );
            }
            return sb.toString ();
      }

      /**
       *
       * @param <Type> param data
       * <p/>
       * @param data
       *
       * @return
       */
      @SuppressWarnings ( "unchecked" )
      public static synchronized <Type> Type byte2Object ( byte[] data ) {
            ByteArrayInputStream bos = new ByteArrayInputStream ( data );
            Type re = null;
            try {
                  ObjectInputStream oos = new ObjectInputStream ( bos );
                  re = ( Type ) oos.readObject ();
            } catch ( ClassNotFoundException | IOException ex ) {
                  System.err.println ( ex );
            }
            return re;
      }

      /**
       *
       * @param <Type> param type
       * <p/>
       * @param type
       *
       * @return
       */
      public static synchronized <Type> byte[] object2Byte ( Type type ) {
            ByteArrayOutputStream bout = new ByteArrayOutputStream ();
            try {
                  ObjectOutputStream oos = new ObjectOutputStream ( bout );
                  oos.writeObject ( type );
            } catch ( IOException ex ) {
                  Logger.getLogger ( To.class.getName () ).log ( Level.SEVERE , null , ex );
            }
            return bout.toByteArray ();

      }

      /**
       *
       * @param <Type>
       * @param set
       *
       * @return
       */
      public static synchronized <Type> List<Type> set2List ( Set<Type> set ) {
            List<Type> list = new ArrayList<> ( 1989 );
            for ( Type object : set ) {
                  list.add ( object );
            }
            return list;
      }

      /**
       *
       * @param longNum < p/>
       * <p/>
       * @return
       */
      public static synchronized Integer num2Integer ( Long longNum ) {
            return longNum.intValue ();
      }

      /**
       *
       * @param doubleNum
       *
       * @return
       */
      public static synchronized Integer num2Integer ( Double doubleNum ) {
            return doubleNum.intValue ();
      }

      /**
       *
       * @param date < p/>
       * <p/>
       * @return
       */
      public static synchronized Time string2Time ( String date ) {
            return Time.born ( string2Date ( date ).getTime () );
      }

      /**
       *
       * @param date
       *
       * @return
       */
      public static synchronized Color string2Color ( String date ) {
            Integer r = Integer.valueOf ( date.substring ( 0 , 2 ) , 16 );
            Integer g = Integer.valueOf ( date.substring ( 2 , 4 ) , 16 );
            Integer b = Integer.valueOf ( date.substring ( 4 , 6 ) , 16 );
            return new Color ( r , g , b );
      }

      /**
       *
       * @param <Key>
       * @param <Val>
       * @param t
       * @param k
       *
       * @return
       */
      public static synchronized <Key , Val> Map<Key , Val> toMap ( Key t , Val k ) {
            Map<Key , Val> map = new HashMap<> ( 10 );
            map.put ( t , k );
            return map;
      }

      /**
       *
       * @param <Key>
       * @param <Val>
       * @param maps
       *
       * @return
       */
      @SafeVarargs
      public static synchronized <Key , Val> Map<Key , Val> toMap ( Map<Key , Val>... maps ) {
            Map<Key , Val> map = new HashMap<> ( 10 );
            map.putAll ( map );
            return map;
      }

      /**
       *
       * @param array
       * @return
       */
      public static synchronized Character[] charArray2CharacterArray ( char[] array ) {
            Integer len = array.length;
            Character[] re = new Character[ len ];
            for ( int x = 0 ; x < len ; x ++ ) {
                  re[x] = array[x];
            }
            return re;
      }

      /**
       *
       * @param array < p/>
       * <p/>
       * @return
       */
      public static synchronized char[] characterArray2CharArray ( Character[] array ) {
            Integer len = array.length;
            char[] re = new char[ len ];
            for ( int x = 0 ; x < len ; x ++ ) {
                  re[x] = array[x];
            }
            return re;
      }

      /**
       *
       * @param str < p/>
       * <p/>
       * @return
       */
      public static synchronized Character[] string2CharacterArray ( String str ) {
            return charArray2CharacterArray ( str.toCharArray () );
      }

      /**
       *
       * @param date < p/>
       * <p/>
       * @return
       */
      public static synchronized Date string2Date ( String date ) {
            try {
                  for ( String regex : STRING_REGEX ) {
                        if ( regex.length () == date.length () ) {
                              return new SimpleDateFormat ( regex ).parse ( date );
                        }
                  }
            } catch ( ParseException ex ) {
            }
            return null;
      }

      /**
       *
       * @param <Type>
       * @param array
       *
       * @return
       */
      @SafeVarargs
      public static synchronized <Type> List<Type> objs2List ( Type... array ) {
            return array2List ( array );
      }

      /**
       * 无限参数，转成数组
       *
       * @param <Type>
       * @param array
       *
       * @return
       */
      @SafeVarargs
      public static synchronized <Type> Type[] objs2Array ( Type... array ) {
            return array;
      }

      /**
       *
       * @param <Type>
       * @param array
       *
       * @return
       */
      @SafeVarargs
      public static synchronized <Type> Stack<Type> objs2Stack ( Type... array ) {
            Stack<Type> stack = new Stack<> ();
            stack.addAll ( Arrays.asList ( array ) );
            return stack;
      }

      /**
       *
       * @param <Type>
       * @param list
       *
       * @return
       */
      public static synchronized <Type> Type[] list2Array ( List<Type> list ) {
            if ( list != null && list.size () > 0 ) {
                  Type[] array = Maker.makeArray ( list.get ( 0 ).getClass () , list.size () );
                  return list.toArray ( array );
            } else {
                  return null;
            }
      }

}
